package com.kedsoft.docms.controller;


import com.kedsoft.docms.common.MessageResult;
import com.kedsoft.docms.entity.Tag;
import com.kedsoft.docms.service.TagService;
import com.kedsoft.docms.vo.PageVo;
import com.kedsoft.docms.vo.Pagination;
import com.kedsoft.docms.vo.TagSelectVo;
import com.kedsoft.docms.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author LGP
 * @since 2018-12-03
 */
@Controller
@RequestMapping("/tag")
public class TagController {
    @Autowired
    private TagService tagService;


    @RequestMapping(value = "index")
    public String index() {
        return "tag/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Tag> getPageList(Pagination<Tag> pagination, @RequestParam(required = false) String tagname){
        Map<String, Object> query = new HashMap<>();
        query.put("tagname", tagname);
        tagService.pageList(pagination,query);
        PageVo<Tag> pageVo = new PageVo<>(pagination.getItems(),pagination.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "all")
    @ResponseBody
    public List<TagSelectVo> listAll() {
        return tagService.getAll();
    }

    @RequestMapping(value = "form")
    public String form(){
        return "tag/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(TagVo param){
        Tag bean = new Tag();
        BeanUtils.copyProperties(param,bean);
        bean.setCrtime(new Date());
        tagService.insert(bean);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(TagVo param){
        Tag bean = new Tag();
        BeanUtils.copyProperties(param,bean);
        bean.setUptime(new Date());
        tagService.updateById(bean);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        tagService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        String[] id = ids.split(",");
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            tagService.deleteById(intId);
        }
        return MessageResult.getSuccess("数据已删除");
    }
}

